package com.core.manager.impl;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.common.exception.RepeatedIndexException;
import com.common.page.Pagination;
import com.core.dao.ActivityTypeDAO;
import com.core.dao.ActivityTypeDAOImpl;
import com.core.entity.ActivityType;
import com.core.entity.ActivityTypeExample;
import com.core.entity.ActivityTypeExample.Criteria;
import com.core.manager.ActivityTypeMng;
@Service
public class ActivityTypeMngImpl implements ActivityTypeMng {

	@Resource(type=ActivityTypeDAOImpl.class)
	private ActivityTypeDAO activityTypeDao;
	
	@Override
	public void save(ActivityType activity) throws RepeatedIndexException {
		Assert.notNull(activity);
		if(repeatedTypeName(activity)){
			throw new RepeatedIndexException("该类型已存在");
		}
		activityTypeDao.insert(activity);
	}

	@Override
	public Pagination getPage(Pagination page) {
		ActivityTypeExample example=new ActivityTypeExample();
		example.setOrderByClause("id");
		return activityTypeDao.getPage(page, example);
	}

	@Override
	public void delete(Integer id) {
		activityTypeDao.deleteByPrimaryKey(id);
	}

	@Override
	public void edit(ActivityType activity) throws RepeatedIndexException {
		if(repeatedTypeName(activity)){
			throw new RepeatedIndexException("该类型已存在");
		}
		activityTypeDao.updateByPrimaryKey(activity);
	}
	
	private boolean repeatedTypeName(ActivityType activity){
		String typeName=activity.getTypeName();
		ActivityTypeExample example=new ActivityTypeExample();
		Criteria c=example.createCriteria();
		c.andTypeNameEqualTo(typeName);
		int result=activityTypeDao.countByExample(example);
		if(result>0){
			return true;
		}
		return false;
	}

}
